local skel = fk.CreateSkill {
  name = "efengqi__dawu",
}

Fk:loadTranslationTable{
  ["efengqi__dawu"] = "大雾",
  [":efengqi__dawu"] = "出牌阶段限一次，你可以弃置任意张“星”，令等量名角色防止受到非雷电伤害，直到你下回合开始。",

  ["@@efengqi__dawu"] = "大雾",
  ["#efengqi__dawu"] = "大雾:弃置任意张“星”，令等量名角色防止受到非雷电伤害，直到你下回合开始",

  ["$efengqi__guanxing_dawu1"] = "此计可保你一时平安。",
  ["$efengqi__guanxing_dawu2"] = "此非万全之策，唯惧天雷。",
}

skel:addEffect("active", {
  prompt = "#efengqi__dawu",
  min_card_num = 1,
  min_target_num = 1,
  card_filter = function (self, player, to_select)
    return not player:prohibitDiscard(to_select) and Fk:getCardById(to_select):getMark("@@efengqi__star") ~= 0
  end,
  target_filter = function (self, player, to_select, selected, cards)
    return #selected < #cards
  end,
  can_use = function (self, player)
    return player:usedSkillTimes(skel.name, Player.HistoryPhase) == 0
  end,
  feasible = function (self, player, selected, cards)
    return #selected > 0 and #selected == #cards
  end,
  on_use = function (self, room, use)
    local player = use.from
    room:throwCard(use.cards, skel.name, player, player)
    for _, to in ipairs(use.tos) do
      if not to.dead and not player.dead then
        room:addTableMarkIfNeed(to, "@@efengqi__dawu", player.id)
      end
    end
  end,
})

skel:addEffect(fk.DamageInflicted, {
  is_delay_effect = true,
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and data.damageType ~= fk.ThunderDamage and #target:getTableMark("@@efengqi__dawu") > 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    data:preventDamage()
  end,
})

---@param player ServerPlayer
local function clearMark(player)
  local room = player.room
  for _, target in ipairs(room.alive_players) do
    local mark = target:getTableMark("@@efengqi__dawu")
    local new = table.filter(mark, function(id) return id ~= player.id end)
    if #new < #mark then
      room:setPlayerMark(target, "@@efengqi__dawu", #new > 0 and new or 0)
    end
  end
end

skel:addLoseEffect(function (self, player, is_death)
  clearMark(player)
end)

skel:addEffect(fk.TurnStart, {
  can_refresh = function (self, event, target, player, data)
    return target == player
  end,
  on_refresh = function (self, event, target, player, data)
    clearMark(player)
  end,
})

--- 仅限一回合的大雾
skel:addEffect(fk.TurnEnd, {
  can_refresh = Util.TrueFunc,
  late_refresh = true,
  on_refresh = function (self, event, _, player, data)
    local mark = player:getTableMark("@@efengqi__dawu")
    local new = table.filter(mark, function(id) return id ~= "-turn" end)
    if #new < #mark then
      player.room:setPlayerMark(player, "@@efengqi__dawu", #new > 0 and new or 0)
    end
  end,
})

return skel
